package com.demomap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.demomap.localservice.LocationService;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MapActivity extends AppCompatActivity {

    @Bind(R.id.mapView)
    MapView mapView;
    @Bind(R.id.btn_loc)
    Button btnLoc;
    @Bind(R.id.tv_loc)
    TextView tvLoc;
    private BaiduMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        ButterKnife.bind(this);

        map = mapView.getMap();
        map.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        map.setTrafficEnabled(true);
        UiSettings settings = map.getUiSettings();
        settings.setAllGesturesEnabled(true);
        settings.setOverlookingGesturesEnabled(true);
        settings.setRotateGesturesEnabled(false);
        settings.setZoomGesturesEnabled(true);
        settings.setScrollGesturesEnabled(true);

        mapView.showZoomControls(false);//地图缩放控件

    }

    @OnClick(R.id.btn_loc)
    public void onClick() {
        locationService.start();// 定位SDK

    }

    //location
    private LocationService locationService;

    @Override
    public void onStop() {
        locationService.unregisterListener(mListener); //注销掉监听

        super.onStop();
    }

    @Override
    public void onStart() {
        super.onStart();
        locationService = MyApp.getInstance().locationService;
        locationService.registerListener(mListener);
        locationService.setLocationOption(locationService.getDefaultLocationClientOption());

    }

    private BDLocationListener mListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {

            // map view 销毁后不在处理新接收的位置
            if (location == null || mapView == null)
                return;
            // 开启定位图层
            map.setMyLocationEnabled(true);
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(0).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            map.setMyLocationData(locData);

            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            map.setMapStatus(u);
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(ll).zoom(19f);
            map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));


            tvLoc.setText(location.getAddrStr());
            locationService.stop(); //停止定位服务
        }
    };
}
